package edu.jptest.action;

import com.opensymphony.xwork2.ActionSupport;

import edu.jptest.bean.User;
import edu.jptest.serviceImp.UserService;

public class RegisterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8571673309067421665L;
	private User user;
	private String vercode; // 验证码
	private String repwd; // 重复密码
	private String email;
	private String validateCode;

	public String getVercode() {
		if (null == vercode)
			return null;
		return vercode.toLowerCase();
	}

	public void setVercode(String vercode) {
		this.vercode = vercode;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getRepwd() {
		return repwd;
	}

	public void setRepwd(String repwd) {
		this.repwd = repwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	// 注册用户
	public String register() {
		UserService userservice = new UserService();
		try {
			if (userservice.UserNameOk(user.getUserNickname())) {
				if (userservice.insertUser(user.getUserNickname(), user
						.getUserPassword(), user.getUserEmail())) {
					return SUCCESS;
				} else {
					this.addActionError("注册用户错误！");
					return ERROR;
				}
			} else {
				this.addActionError("用户名已被占用，请换个用户名！");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	// 激活用户
	public String activateUser() {
		UserService userService = new UserService();
		System.out.println("激活用户");
		System.out.println(email + " ==== " + validateCode);
		if (userService.regValidate(email, validateCode)) {
			if (userService.activateRegUser(email)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		}
		return ERROR;
	}
}
